/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.gs.service.impl;

import me.zhengjie.gs.domain.GShelves;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.gs.repository.GShelvesRepository;
import me.zhengjie.gs.service.GShelvesService;
import me.zhengjie.gs.service.dto.GShelvesDto;
import me.zhengjie.gs.service.dto.GShelvesQueryCriteria;
import me.zhengjie.gs.service.mapstruct.GShelvesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
 * @website https://eladmin.vip
 * @description 服务实现
 * @author yun
 * @date 2024-02-20
 **/
@Service
@RequiredArgsConstructor
public class GShelvesServiceImpl implements GShelvesService {
    
    private final GShelvesRepository gShelvesRepository;
    private final GShelvesMapper gShelvesMapper;
    
    @Override
    public PageResult<GShelvesDto> queryAll(GShelvesQueryCriteria criteria, Pageable pageable){
        Page<GShelves> page = gShelvesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gShelvesMapper::toDto));
    }
    
    @Override
    public List<GShelvesDto> queryAll(GShelvesQueryCriteria criteria){
        return gShelvesMapper.toDto(gShelvesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
    
    @Override
    @Transactional
    public GShelvesDto findById(Integer gsId) {
        GShelves gShelves = gShelvesRepository.findById(gsId).orElseGet(GShelves::new);
        ValidationUtil.isNull(gShelves.getGsId(),"GShelves","gsId",gsId);
        return gShelvesMapper.toDto(gShelves);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(GShelves resources) {
        gShelvesRepository.save(resources);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GShelves resources) {
        GShelves gShelves = gShelvesRepository.findById(resources.getGsId()).orElseGet(GShelves::new);
        ValidationUtil.isNull( gShelves.getGsId(),"GShelves","id",resources.getGsId());
        gShelves.copy(resources);
        gShelvesRepository.save(gShelves);
    }
    
    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer gsId : ids) {
            gShelvesRepository.deleteById(gsId);
        }
    }
    
    @Override
    public void download(List<GShelvesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GShelvesDto gShelves : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("货架名称", gShelves.getGsName());
            map.put("货架样式", gShelves.getGsStyle());
            map.put("轮播图", gShelves.getGsImg());
            map.put("背景图片", gShelves.getGsImage());
            map.put("货架类型", gShelves.getGsType());
            map.put("背景图片名称", gShelves.getGsImagepath());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
